package src.threadlocal;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 1000个打印日期任务，用线程池来执行
 */
public class ThreadLocalNormalUsage04 {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public String date(int seconds) {
        //参数的定位是毫秒，从1970年1月1日00:00:00到现在的秒数
        Date date = new Date(seconds * 1000L);
        String s = null;
        synchronized (ThreadLocalNormalUsage04.class) {
            s = dateFormat.format(date);
        }
        return s;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    String date = new ThreadLocalNormalUsage04().date(finalI);
                    System.out.println(date);
                }
            });
        }
        executorService.shutdown();
    }
}
